//
//  TYPlayer.m
//  AngerDragon
//
//  Created by gaotime_macbook_2 on 11-12-6.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "TYPlayer.h"

/// 状态机系统
#import "TYStateSystemProtocol.h"
#import "TYStateSystemFactory.h"

/// 生命系统相关
#import "TYPlayerLifeSystemProtocol.h"
#import "TYPlayerLifeSystemFactory.h"

static TYPlayer *m_p_cls_player;

@implementation TYPlayer

@synthesize m_display_system_protocol;

- (id) init {
	 
	if ((self = [super init]))
	{
		CCLOG(@"%@: %@", NSStringFromSelector(_cmd), self);
		
		NSAssert(m_p_cls_player == nil, @"重复init player对象!");
		m_p_cls_player = self;
		
		/// 通过工厂方法, 创建状态机系统
		TYStateSystemFactory *a_p_cls_state_system_factory = [[TYStateSystemFactory alloc]init];
		m_state_system_protocol = [a_p_cls_state_system_factory creatStateSystem];
		[a_p_cls_state_system_factory release];
		
		/// 通过工厂方法, 创建生命系统
		TYPlayerLifeSystemFactory *a_p_cls_life_system_factory = [[TYPlayerLifeSystemFactory alloc]init];
		m_life_system_protocol = (id<TYPlayerLifeSystemProtocol>)[a_p_cls_life_system_factory creatLifeSystem];
		[a_p_cls_life_system_factory release];
		
	}
	
	return self;
}

- (void) dealloc {
	CCLOG(@"%@: %@", NSStringFromSelector(_cmd), self);
	
	m_p_cls_player = nil;
	
	[m_state_system_protocol deallocMe];
	[m_life_system_protocol deallocMe];
	[super dealloc];
}

+ (id<TYPlayerCommunicateProtocol>) getPlayerCommunicateProtocol {
	NSAssert(m_p_cls_player != nil, @"getPlayerCommunicateProtocol not available!");
	return m_p_cls_player;
}

#pragma mark 实现精灵AI接口
- (void) ai:(ccTime)delta {
}

#pragma mark 实现碰撞接口
- (CGRect)getCollisionRect {
	
	return CGRectMake(0, 0, 10, 10);
	//Rect *a_p_collision_rect = [[Rect alloc]init autorelease];
}

#pragma mark 实现被伤害接口
- (NSInteger) beHurt:(NSInteger) a_n_hurt_value {
	return 100;
} 

#pragma mark 实现外界跟player沟通的协议接口
/// 判断是否已经死亡
- (BOOL) player_isDead {
	return NO;
}
/// 增加player剩余生命值
- (void) player_increaseRemainLife:(NSInteger) a_n_value {
}
/// 增加player生命值上线
- (void) player_increaseLimitLife:(NSInteger) a_n_value {
}
/// 伤害player(本接口的返回值, 就是player的反伤值, 这里考虑很多近战怪物攻击player后, player会反伤它们)
- (NSInteger) player_hurtPlayer:(NSInteger) a_n_hurt_value {
	return 10;
}
/// 增加player金钱数
- (void) player_addMoney:(NSInteger) a_n_money {
}
/// 增加 可以继续游戏的次数(考虑玩家购买剩余游戏次数的情况, 一次买多个)
- (void) player_addContinueGameTime:(NSInteger) a_n_add_times {
}
/// 减少 可以继续游戏的次数(每次减少1次游戏次数)
- (void) player_subContinueGameTime {
}
/// 获取剩余的游戏次数
- (NSInteger) player_getContinueGameTime {
	return 0;
}

/// 获取player的碰撞矩形
- (CGRect) player_getCollisionRect {
	return [self getCollisionRect];
}

/// 敌人获取能够对player造成有效攻击的点坐标, 根据其攻击距离来计算(敌人要移动到有效攻击点, 才能进行攻击行为)
- (CGPoint) player_getEffectivePointOfAttack:(CGPoint) a_st_enemy_sprite_point 
															attackDistance:(CGFloat) a_f_attack_distance {
	return ccp(0, 240);
}
/// 敌人精灵根据其攻击距离和其当前坐标, 判断是否可以开始攻击player
- (BOOL) player_isCanAttackPlayer:(CGPoint) a_st_enemy_sprite_point 
									 attackDistance:(CGFloat) a_f_attack_distance {
	/// 这里的算法是, 既然计算player的有效攻击圆形的坐标不好计算, 我们可以换种思路,
  /// 敌对精灵出生后, 就朝着player中心点的坐标移动, 当它和player中心点距离小于它的攻击距离+player的有效被攻击半径,
	/// 就证明敌对精灵已经进入了有效的攻击区域, 就可以开始攻击player了
	CGFloat a_f_distance = ccpDistance(a_st_enemy_sprite_point, [self player_getPointOfCenter]);
	if (a_f_distance <= a_f_attack_distance + 20) {
		return YES;
	} else {
		return NO;
	}
}

/// 获取player的中心点坐标
- (CGPoint) player_getPointOfCenter {
	return ccp(0, 240);
}
#pragma mark -------------------------
@end
